package org.project.product.android.pfe_fanta;

import org.project.product.android.pfe.fanta.R;
import org.project.product.android.pfe.fanta.adapter.DataManagerFacade;
import org.project.product.android.pfe.fanta.domain.DeleteProductDialog;
import org.project.product.android.pfe.fanta.domain.Product;
import org.project.product.android.pfe.fanta.util.Utils;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class EditOrDeleteProduct extends ActivityParent {

	private Product product;
	public static int EDITED = 1213;
	public static int DELETED = 1212;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		this.setContentView(R.layout.edit_product);
		product = (Product) getIntent().getExtras().getSerializable(
		        Utils.PRODUCT_SELECTED);
		initWithProduct();
	}

	private void initWithProduct() {
		EditText name = (EditText) findViewById(R.id.edit_name_produit);
		EditText poids = (EditText) findViewById(R.id.edit_product_poids);
		EditText description = (EditText) findViewById(R.id.edit_description);
		EditText codeBarre = (EditText) findViewById(R.id.edit_code_barre);
		EditText quantity = (EditText) findViewById(R.id.edit_product_quantiy);
		EditText prix = (EditText) findViewById(R.id.edit_prix);
		quantity.setText(String.valueOf(product.getQuantity()));
		name.setText(this.product.getNom());
		poids.setText(String.valueOf(this.product.getPoids()));
		description.setText(this.product.getDescription());
		prix.setText(String.valueOf(product.getPrix()));
		codeBarre.setText(this.product.getCodBarre());
	}

	/**
	 * 
	 * @param view
	 */
	public final void deleteProduct(final View view) {
		DeleteProductDialog deleteProductDialog = new DeleteProductDialog(this,
		        product);
		deleteProductDialog.show();

	}

	public void validProduct(View view) {
		EditText name = (EditText) findViewById(R.id.edit_name_produit);
		EditText poids = (EditText) findViewById(R.id.edit_product_poids);
		EditText description = (EditText) findViewById(R.id.edit_description);
		EditText codeBarre = (EditText) findViewById(R.id.edit_code_barre);
		EditText prix = (EditText) findViewById(R.id.edit_prix);
		EditText quantity = (EditText) findViewById(R.id.edit_product_quantiy);

		if (Utils
		        .checkEditText(this, name, poids, description, codeBarre, prix)) {

			String codeBarreValue = codeBarre.getText().toString();
			String desc = description.getText().toString();
			Product product = new Product();
			product.setCodBarre(codeBarreValue);
			product.setNom(name.getText().toString());
			product.setDescription(desc);

			product.setIdCategory(this.product.getIdCategory());
			product.setId(this.product.getId());
			try {
				product.setPrix(Double.parseDouble(prix.getText().toString()));
				product.setQuantity(Integer.parseInt(quantity.getText()
				        .toString()));

				double poidValue = Double.parseDouble(poids.getText()
				        .toString());
				product.setPoids(poidValue);
				DataManagerFacade.getInstance(getApplicationContext())
				        .saveProduct(product);
			} catch (NumberFormatException exception) {
				Toast.makeText(this, "Le poids est incorrecte",
				        Toast.LENGTH_LONG).show();
				return;
			}
			Intent intent = new Intent();
			intent.putExtra(Utils.PRODUCT_SELECTED, product);
			this.setResult(EDITED, intent);
			finish();
			Toast.makeText(getApplicationContext(),
			        "Produit mise a jour avec succes", Toast.LENGTH_SHORT)
			        .show();
		}
	}

	private void confirmDelete() {

		Builder dialog = new Builder(this);
		dialog.setTitle("Confirmation");
		dialog.setMessage("Voulez vous supprimer ce produit");
		dialog.setPositiveButton("OK", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				DataManagerFacade.getInstance(getApplicationContext())
				        .deleteProduct(product);
				backHome();

			}
		});
		dialog.setNegativeButton("Annuler", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();

			}
		});
		dialog.show();
	}

	private void backHome() {
		Intent intent = new Intent(this, Home.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}
}
